package com.yingsheng.bi.hks.web.component;

import com.yingsheng.bi.hks.basic.ReqContext;
import com.yingsheng.bi.hks.basic.consts.NameDict;
import com.yingsheng.bi.hks.basic.utils.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Web拦截打印业务名称
 *
 * @author root
 */
@Slf4j
public class AccessTokenInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String accessToken = HttpUtils.getRequestHeader(NameDict.ACCESS_TOKEN);
        if (log.isInfoEnabled()) {
            log.info("AccessToken [" + accessToken + "]");
        }
        return true;
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, @Nullable Exception ex) throws Exception {
        try {
            ReqContext.reset();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

}
